package com.whut.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 对字符串或集合进行相关操作的类
 *
 * @author fanyu
 * @date 2019/9/7 10:49
 */
public class StringOperate {
    /**
     * 用分隔符把list中的数据进行分割，返回一整条以分隔符分好的字符串
     *
     * @return java.lang.String
     * @author fanyu
     * @date 2019/9/7 10:53
     * @params [list, separator]
     */
    public static String listToString(List<String> list, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(separator);
        }
        return sb.toString().substring(0, sb.toString().length() - 1);
    }

    /**
     * 将list中的数据连乘并返回开n次方后的结果
     *
     * @return float
     * @author fanyu
     * @date 2019/9/18 11:04
     * @params [list]
     */
    public static float multiply(List<Float> list) {
        float result = 1f;
        for (int i = 0; i < list.size(); i++) {
            result *= list.get(i);
        }
        BigDecimal b = new BigDecimal(String.valueOf(result));
        double d = b.doubleValue();
        double dd = Math.pow(d, (double) 1 / list.size());
        result = (float) dd;
        return result;
    }

    public static List<Float> normalization(List<Float> list) {
        List<Float> result = new ArrayList<>();
        float sum = 0;
        for (int i = 0; i < list.size(); i++) {
            sum += list.get(i);
        }
        //先判断，0不能做分母
        if(sum!=0){
            for (int i = 0; i < list.size(); i++) {
                result.add(list.get(i) / sum);
            }
        }else{
            for (int i = 0; i < list.size(); i++) {
                result.add(list.get(i));
            }
        }
        return result;
    }

    public static List<Double> normalization2(List<Double> list) {
        List<Double> result = new ArrayList<>();
        float sum = 0;
        for (int i = 0; i < list.size(); i++) {
            sum += list.get(i);
        }
        for (int i = 0; i < list.size(); i++) {
            result.add(list.get(i) / sum);
        }
        return result;
    }

    public static double[][] clusterCaculate(double[][] m,int size)
    {
        double[][] a=new double[size][size];
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            {
                List<Double> list=new ArrayList<>();
                List<Double> list1=new ArrayList<>();
                List<Double> list2=new ArrayList<>();
                for(int k=0;k<size;k++)
                {
                    list1.add(m[i][k]);
                    list2.add(m[k][j]);
                }
                for(int n=0;n<list1.size();n++)
                {
                    if(list1.get(n)<list2.get(n))
                    {
                        list.add(list1.get(n));
                    }else{
                        list.add(list2.get(n));
                    }
                }
               a[i][j]= Collections.max(list);
            }
        }
        return a;
    }

}
